package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.sys.bean.SysNotifyRecord;
import com.fzrskj.cloud.free.sys.dao.SysNotifyRecordDao;
import com.fzrskj.cloud.free.user.bean.UserAccountData;
import com.fzrskj.cloud.free.user.utils.UserAccountDataUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysNotifyRecordUtils extends CObject{
	
	//to get notify record by record id
	public SysNotifyRecord getNotifyRecord(int rid){
		SysNotifyRecordDao dao=new SysNotifyRecordDao();
		SysNotifyRecord ret = dao.querySysNotifyRecord(rid);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return null;
		}
		if (ret==null){
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
			return null;
		}
		return ret;
	}
	
	//query notify record list
	public List<SysNotifyRecord> querySysNotifyRecordList(Map<String, Object> cdt, int page, int pagesize){
		SysNotifyRecordDao dao=new SysNotifyRecordDao();
		List<SysNotifyRecord> ret = dao.querySysNotifyRecordList(cdt, page, pagesize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return null;
		}
		if (ret==null){
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
			return null;
		}
		return ret;
	}
	
	//query notify record Count
	public int querySysNotifyRecordCount(Map<String, Object> cdt){
		SysNotifyRecordDao dao=new SysNotifyRecordDao();
		int ret = dao.querySysNotifyRecordCount(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}
	
	//批量发送邮件
	public String bathSendEmail(String object,String listName,String subject,String message){
		JSONObject result = new JSONObject();
		//全部会员
		if("all".equals(object)){
			Map<String, Object> map = new HashMap<String, Object>();
			UserAccountDataUtils uadUtils = new UserAccountDataUtils();
			List<UserAccountData> list = uadUtils.queryUserAccountdataList(map);
			if(list.size()<=0){
				result.put("code", "-1");
				result.put("list", "");
				return result.toString();
			}

			for(UserAccountData u:list){
				if(!CStrTool.isBlankStr(u.getEmail())){
					SendMailUtils.bathSendMail(u.getEmail(), subject, message);
				}
			}
		}

		//指定会员
		if("userName".equals(object)){
			if(CStrTool.isBlankStr(listName)){
				result.put("code", "-1");
				result.put("listName", "");
				return result.toString();
			}

			UserAccountDataUtils uadUtils = new UserAccountDataUtils();
			String[] arry=listName.split("<br>");
			for(String str:arry){
				UserAccountData uad=uadUtils.queryAccountDataByName(str);
				if(uad==null)
					continue;

				if(!CStrTool.isBlankStr(uad.getEmail())){
					SendMailUtils.bathSendMail(uad.getEmail(), subject, message);
				}
			}
		}

		//指定邮箱
		if("email".equals(object)){
			if(CStrTool.isBlankStr(listName)){
				result.put("code", "-1");
				result.put("listName", "");
				return result.toString();
			}

			String[] arry=listName.split("<br>");
			for(String str:arry){
				if(!CStrTool.isBlankStr(str)){
					SendMailUtils.bathSendMail(str, subject, message);
				}
			}
		}

		//云主机会员
		if("cloud".equals(object)){
			UserAccountDataUtils uadUtils = new UserAccountDataUtils();
			List<UserAccountData> list = uadUtils.queryCloudUadList();
			if(list.size()<=0){
				result.put("code", "-1");
				result.put("list", "");
				return result.toString();
			}

			for(UserAccountData u:list){
				if(!CStrTool.isBlankStr(u.getEmail())){
					SendMailUtils.bathSendMail(u.getEmail(), subject, message);
				}
			}
		}

		//域名会员
		if("domain".equals(object)){
			UserAccountDataUtils uadUtils = new UserAccountDataUtils();
			List<UserAccountData> list = uadUtils.queryDomainUadList();
			if(list.size()<=0){
				result.put("code", "-1");
				result.put("list", "");
				return result.toString();
			}

			for(UserAccountData u:list){
				if(!CStrTool.isBlankStr(u.getEmail())){
					SendMailUtils.bathSendMail(u.getEmail(), subject, message);
				}
			}
		}

		//消费会员
		if("consume".equals(object)){
			UserAccountDataUtils uadUtils = new UserAccountDataUtils();
			List<UserAccountData> list = uadUtils.queryConsumeUadList();
			if(list.size()<=0){
				result.put("code", "-1");
				result.put("list", "");
				return result.toString();
			}
			
			for(UserAccountData u:list){
				if(!CStrTool.isBlankStr(u.getEmail())){
					SendMailUtils.bathSendMail(u.getEmail(), subject, message);
				}
			}
		}
	
		result.put("code", "0");
		return result.toString();
	}
}